import os
import shutil
from tkinter import filedialog, Tk, Button, Label, messagebox


def contains_keywords(filename):
    # 定义年份、省份、城市、年级和学科的匹配标准
    # years = ["24春", "24秋"]
    # provinces = ["湖北省", "广东省", "北京市"]  # 根据需要添加更多的省份
    # cities = ["武汉市", "深圳市", "北京市"]  # 根据需要添加更多的城市
    # grades = ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级"]  # 年级
    # subject = "数学"
    years = ["24春", "24秋","2023-2024","2024春","2024秋"]
    provinces = ["北京市","天津市","上海市","重庆市","河北省","山西省","辽宁省","吉林省","黑龙江省","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","海南省","四川省","贵州省","云南省","陕西省","甘肃省","青海省","台湾省","内蒙古自治区","广西壮族自治区","西藏自治区","宁夏回族自治区","新疆维吾尔自治区","香港特别行政区","澳门特别行政区"]  # 根据需要添加更多的省份
    cities = ["石家庄市","唐山市","秦皇岛市","邯郸市","邢台市","保定市","张家口市","承德市","沧州市","廊坊市","衡水市","太原市","大同市","朔州市","阳泉市","长治市","晋城市","忻州市","吕梁市","晋中市","临汾市","运城市","呼和浩特市","包头市","乌海市","赤峰市","通辽市","鄂尔多斯市","呼伦贝尔市","巴彦淖尔市","乌兰察布市","兴安盟市","锡林郭勒盟市","阿拉善盟市","沈阳市","大连市","鞍山市","抚顺市","本溪市","丹东市","锦州市","营口市","阜新市","辽阳市","盘锦市","铁岭市","朝阳市","葫芦岛市","长春市","吉林市","四平市","辽源市","通化市","白山市","松原市","白城市","哈尔滨市","齐齐哈尔市","鹤岗市","双鸭山市","鸡西市","大庆市","伊春市","牡丹江市","佳木斯市","七台河市","黑河市","绥化市","南京市","无锡市","徐州市","常州市","苏州市","南通市","连云港市","淮安市","盐城市","扬州市","镇江市","泰州市","宿迁市","杭州市","宁波市","温州市","嘉兴市","湖州市","绍兴市","金华市","衢州市","舟山市","台州市","丽水市","合肥市","芜湖市","蚌埠市","淮南市","马鞍山市","淮北市","铜陵市","安庆市","黄山市","滁州市","宿州市","池州市","宣城市","六安市","阜阳市","亳州市","福州市","厦门市","莆田市","三明市","泉州市","漳州市","南平市","龙岩市","宁德市","南昌市","景德镇市","萍乡市","九江市","新余市","鹰潭市","赣州市","吉安市","宜春市","抚州市","上饶市","济南市","青岛市","淄博市","枣庄市","东营市","烟台市","潍坊市","济宁市","泰安市","威海市","日照市","莱芜市","临沂市","德州市","聊城市","滨州市","菏泽市","郑州市","开封市","洛阳市","平顶山市","安阳市","鹤壁市","新乡市","焦作市","濮阳市","许昌市","漯河市","三门峡市","南阳市","商丘市","信阳市","周口市","驻马店市","济源市","武汉市","黄石市","十堰市","宜昌市","襄阳市","鄂州市","荆门市","孝感市","荆州市","黄冈市","咸宁市","随州市","恩施土家族苗族自治州市","仙桃市","潜江市","天门市","神农架林区市","长沙市","株洲市","湘潭市","衡阳市","邵阳市","岳阳市","常德市","张家界市","益阳市","郴州市","永州市","怀化市","娄底市","湘西土家族苗族自治州市","广州市","韶关市","深圳市","珠海市","汕头市","佛山市","江门市","湛江市","茂名市","肇庆市","惠州市","梅州市","汕尾市","河源市","阳江市","清远市","东莞市","中山市","潮州市","揭阳市","云浮市","海口市","三亚市","三沙市","儋州市","五指山市","琼海市","文昌市","万宁市","东方市","定安县","屯昌县","澄迈县","临高县","白沙黎族自治县市","昌江黎族自治县市","乐东黎族自治县市","陵水黎族自治县市","保亭黎族苗族自治县市","琼中黎族苗族自治县市","成都市","自贡市","攀枝花市","泸州市","德阳市","绵阳市","广元市","遂宁市","内江市","乐山市","南充市","眉山市","宜宾市","广安市","达州市","雅安市","巴中市","资阳市","阿坝藏族羌族自治州市","甘孜藏族自治州市","凉山彝族自治州市","贵阳市","六盘水市","遵义市","安顺市","毕节市","铜仁市","黔西南布依族苗族自治州市","黔东南苗族侗族自治州市","黔南布依族苗族自治州市","昆明市","曲靖市","玉溪市","保山市","昭通市","丽江市","普洱市","临沧市","楚雄彝族自治州市","红河哈尼族彝族自治州市","文山壮族苗族自治州市","西双版纳傣族自治州市","大理白族自治州市","德宏傣族景颇族自治州市","怒江傈僳族自治州市","迪庆藏族自治州市","西安市","铜川市","宝鸡市","咸阳市","渭南市","延安市","汉中市","榆林市","安康市","商洛市","兰州市","嘉峪关市","金昌市","白银市","天水市","武威市","张掖市","平凉市","酒泉市","庆阳市","定西市","陇南市","临夏回族自治州市","甘南藏族自治州市","西宁市","海东市","海北藏族自治州市","黄南藏族自治州市","海南藏族自治州市","果洛藏族自治州市","玉树藏族自治州市","海西蒙古族藏族自治州市","台北市","高雄市","台中市","台南市","新北市","基隆市","新竹市","嘉义市","桃园市","苗栗市","彰化市","南投市","云林市","屏东县市","宜兰市","花莲市","台东市","澎湖市","金门市","连江县","香港特别行政区","澳门特别行政区"]  # 根据需要添加更多的城市
    grades = ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级"]  # 年级
    subject = "数学"

    # 使用布尔变量记录是否满足条件
    has_year = any(year in filename for year in years)
    has_province = any(province in filename for province in provinces)
    has_city = any(city in filename for city in cities)
    has_grade = any(grade in filename for grade in grades)
    has_subject = subject in filename

    if has_year and has_province and has_city and has_grade and has_subject:
        return {
            'year': next(year for year in years if year in filename),
            'province': next(province for province in provinces if province in filename),
            'city': next(city for city in cities if city in filename),
            'grade': next(grade for grade in grades if grade in filename)
        }
    return None


def classify_file(src, dest):
    for root, _, files in os.walk(src):  # 使用os.walk递归遍历
        for filename in files:
            file_path = os.path.join(root, filename)
            keyword_matches = contains_keywords(filename)

            if keyword_matches:
                year = keyword_matches['year']
                province = keyword_matches['province']
                city = keyword_matches['city']
                grade = keyword_matches['grade']

                # 创建符合目录格式的目标目录：年份 > 年级 > 省份 > 市
                target_dir = os.path.join(dest, year, grade, province, city)
                os.makedirs(target_dir, exist_ok=True)

                dest_path = os.path.join(target_dir, filename)
                shutil.copy2(file_path, dest_path)
                print(f"Copied {file_path} to {dest_path}")


def select_directory(title):
    root = Tk()
    root.withdraw()  # 隐藏主窗口
    directory = filedialog.askdirectory(title=title)
    root.destroy()
    return directory


class App:
    def __init__(self, master):
        self.master = master
        master.title("文件分类器")

        self.source_directory = ""
        self.destination_directory = ""

        self.label = Label(master, text="请选择源目录和目标目录")
        self.label.pack()

        self.select_source_button = Button(master, text="选取源目录", command=self.select_source)
        self.select_source_button.pack()

        self.select_destination_button = Button(master, text="选取目标目录", command=self.select_destination)
        self.select_destination_button.pack()

        self.run_button = Button(master, text="开始分类", command=self.run_classification)
        self.run_button.pack()

    def select_source(self):
        self.source_directory = select_directory("选择源目录")
        messagebox.showinfo("提示", f"源目录已选择: {self.source_directory}")

    def select_destination(self):
        self.destination_directory = select_directory("选择目标目录")
        messagebox.showinfo("提示", f"目标目录已选择: {self.destination_directory}")

    def run_classification(self):
        if not self.source_directory or not self.destination_directory:
            messagebox.showwarning("警告", "请先选择源目录和目标目录！")
            return

        classify_file(self.source_directory, self.destination_directory)
        messagebox.showinfo("完成", "文件分类完成！")


if __name__ == "__main__":
    root = Tk()
    app = App(root)
    root.mainloop()
